package com.huaxindata.im.model.entity;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;


public class ChatSvgMessageBody extends ChatFileMessageBody implements Parcelable{

    /**
     * 附件是否下载完成
     */
    public int downloadStatus;

    /**
     * 缩略图本地文件路径
     */
    public String thumbLocalPath;

    /**
     * 缩略图服务器远程文件路径
     */
    public String thumbRemotePath;

    public String code;

    public String SvgSize;

    public ChatSvgMessageBody() {
    }

    public ChatSvgMessageBody(int type, String code, String url, String name, String size) {
        super(type);
        this.code=code;
        this.remotePath=url;
        this.thumbRemotePath=url;
        this.fileName=name;
        this.SvgSize=size;
    }

    public ChatSvgMessageBody(int type) {
        super(type);
    }

    protected ChatSvgMessageBody(Parcel in) {
        super(in);
        downloadStatus = in.readInt();
        thumbLocalPath = in.readString();
        thumbRemotePath = in.readString();
        code = in.readString();
        SvgSize = in.readString();
    }

    public static final Creator<ChatSvgMessageBody> CREATOR = new Creator<ChatSvgMessageBody>() {
        @Override
        public ChatSvgMessageBody createFromParcel(Parcel in) {
            return new ChatSvgMessageBody(in);
        }

        @Override
        public ChatSvgMessageBody[] newArray(int size) {
            return new ChatSvgMessageBody[size];
        }
    };

    /**
     * @param message
     * @throws NumberFormatException
     */
    public void parseSvgMessage(String message) throws NumberFormatException {
        // 1#&54#&6#&8#&5http://10.0.0.127:8080/uploads/mvg/5/2016919170988.pngqqq工艺图223.60KB
        // code.length,remotePath.length,name.length,size.length,code,remotePath,name,size;
        // code 是 stationcode|devicecode 的拼接
        if (TextUtils.isEmpty(message))
            return;
        String[] words = message.split(ChatMessage.PREX);
        if (words.length < 5) {
            return;
        }
        int clen = Integer.parseInt(words[0]);
        int imagelen = Integer.parseInt(words[1]);
        int nlen = Integer.parseInt(words[2]);
        String content = words[4];
        this.code = content.substring(0, clen);
        this.remotePath = content.substring(clen, clen + imagelen);
        this.thumbRemotePath=this.remotePath;
        this.fileName = content.substring(clen + imagelen, clen + imagelen + nlen);
        this.SvgSize = content.substring(clen + imagelen + nlen);
    }

    /**
     * 发送消息，拼接Svg消息字段
     *
     * @return
     */
    public String matchSvgMessage() {
        return code.length() + ChatMessage.PREX
                + remotePath.length() + ChatMessage.PREX
                + fileName.length() + ChatMessage.PREX
                + SvgSize.length() + ChatMessage.PREX
                + code + remotePath + fileName + SvgSize;
    }


    @Override
    public String toString() {
        return "ChatSvgMessageBody{" +
                "downloadStatus=" + downloadStatus +
                ", thumbLocalPath='" + thumbLocalPath + '\'' +
                ", thumbRemotePath='" + thumbRemotePath + '\'' +
                ", code='" + code + '\'' +
                ", SvgSize='" + SvgSize + '\'' +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        super.writeToParcel(parcel,i);
        parcel.writeInt(downloadStatus);
        parcel.writeString(thumbLocalPath);
        parcel.writeString(thumbRemotePath);
        parcel.writeString(code);
        parcel.writeString(SvgSize);
    }
}
